package com.llkj.contractmanagement.contractmanagement.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.llkj.contractmanagement.main.BaseFragment;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.contractmanagement.adapter.BrowsingHstoryContractAdapter;
import com.llkj.contractmanagement.http.HttpMethod;
import com.llkj.contractmanagement.http.UrlConfig;
import com.llkj.contractmanagement.main.MyApplication;
import com.llkj.contractmanagement.model.MovieContractBean;
import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.utils.GsonUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * author: dong.yj
 * created on: 2016/8/18 14:39
 * description:浏览足迹-合同
 */
public class BrowsingHistoryContractFragment extends BaseFragment implements AdapterView.OnItemClickListener, BGARefreshLayout.BGARefreshLayoutDelegate {
    @Override
    public int getContentViewLayout() {
        return R.layout.fragment_browsinghistory_contract;
    }

    @Bind(R.id.bga_refresh)
    BGARefreshLayout bgaRefreshLayout;
    @Bind(R.id.lv_browsinghistory_contract)
    ListView lv_contract;
    private BrowsingHstoryContractAdapter adapter;
    private List<MovieContractBean.HtlistBean> contractList;
    private UserInfoBean userInfoBean;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    private void init() {
        initData();
        setListener();
    }

    private void initData() {
        bgaRefreshLayout.setRefreshViewHolder(new BGANormalRefreshViewHolder(MyApplication.getInstance(), false));
        userInfoBean = UserInfoBean.getInstance();
        contractList = new ArrayList<>();
        adapter = new BrowsingHstoryContractAdapter(ctx, contractList);
        lv_contract.setAdapter(adapter);
        requestBrowsingHistoryContract();
    }

    private void requestBrowsingHistoryContract() {
        HashMap<String, String> map = new HashMap<>();
//        user_id	1	string	用户的id
//        token	1	string	Token
        map.put("user_id",userInfoBean.getId());
        map.put("token", userInfoBean.getToken());
        HttpMethod.zjht(this, ctx, map, UrlConfig.HTTP_ZJ_HT);
    }


    private void setListener() {
        lv_contract.setOnItemClickListener(this);
        bgaRefreshLayout.setDelegate(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        super.onSuccessHttp(responseInfo, resultCode);
        switch (resultCode) {
            case UrlConfig.HTTP_ZJ_HT:
                bgaRefreshLayout.endRefreshing();
                MovieContractBean movieContractBean = GsonUtil.GsonToBean(responseInfo, MovieContractBean.class);
                if (movieContractBean != null) {
                    List<MovieContractBean.HtlistBean> addList = movieContractBean.getHtlist();
                    if (addList.size() > 0 && addList != null) {
                        contractList.addAll(addList);
                        adapter.notifyDataSetChanged();
                    }
                }
                break;
            case UrlConfig.HTTP_DEL_HTZJ:
                contractList.clear();
                adapter.notifyDataSetChanged();
                break;

        }
    }

    public void cleanDate() {
        HashMap<String, String> map = new HashMap<>();
//        user_id	1	string	用户的id
//        token	1	string	Token
        map.put("user_id",userInfoBean.getId());
        map.put("token", userInfoBean.getToken());
        HttpMethod.delHtzj(this, ctx, map, UrlConfig.HTTP_DEL_HTZJ);
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        contractList.clear();
        requestBrowsingHistoryContract();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }
}
